﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.FindArtInfo;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;
using CommandLib;
using FindArtInfo.Common;
using FindArtInfo.Models;

namespace FindArtInfo.Controllers
{
    public class ArtistsController : MultiBase
    {
        public ActionResult Result(string artistName, int? born, int? die, int? countryId, int? pageIndex)
        {            
            int pageSize = 30;
            PageArtistsSearchResultModel model = new PageArtistsSearchResultModel();            
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            model.ArtistsSearchResultKeyWord = artistName;
            model.ListArtist = new ArtistsBL().Search(artistName, born, die, countryId, Convert.ToInt32(pageIndex == null ? 1 : pageIndex),
                                                      pageSize);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_SEARCH] == null)
            {
                int totalRecord;
                new ArtistsBL().GetCountByKeyWord(artistName, born, die, countryId, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_SEARCH] = totalRecord;                
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_SEARCH]);
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = string.Format("/{0}/{1}?artistName={2}&born={3}&die={4}&countryId={5}", "Artists", "Result",
                                      HttpUtility.UrlEncode(artistName), born, die, countryId);
            return View(model);
        }

        public ActionResult Letter(string letter, int? pageIndex)
        {
            int pageSize = 60;            
            PageArtistLetterModel model = new PageArtistLetterModel();
            model.Letter = string.IsNullOrEmpty(letter) ? "A" : letter;
            model.ListArtists = new ArtistsBL().GetByLastNameLetter(string.IsNullOrEmpty(letter) ? "A" : letter, Convert.ToInt32(pageIndex == null ? 1 : pageIndex), pageSize);
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_LETTER] == null)
            {
                int totalRecord;
                new ArtistsBL().GetCountByLastNameLetter(string.IsNullOrEmpty(letter) ? "A" : letter, out totalRecord);
                Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_LETTER] = totalRecord;
            }
            model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_ARTIST_LETTER]);
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = string.IsNullOrEmpty(letter) ? "/browse-by-artist" : string.Format("/{0}/browse-by-artist", letter);
            return View(model);
        }

        public ActionResult ListPictures(int? itemSelected, int? itemTypeId, int? sort, int? pageIndex)
        {
            int pageSize = 8;
            PageArtistPicturesModel model = new PageArtistPicturesModel();
            List<ItemType> listItemType = new ItemTypeBL().GetAll();
            model.ListItemType = listItemType;            
            model.PageSize = pageSize;
            ItemType itemType = new ItemType();
            if(itemTypeId != null)
            {
                itemType = listItemType.Where(x => x.ID == Convert.ToInt32(itemTypeId)).FirstOrDefault();
                model.ItemTypeName = itemType == null ? string.Empty : itemType.Name;
            }
            model.ItemSelected = itemSelected == null ? 0 : itemSelected;
            model.Order = sort;
            model.ItemTypeId = itemTypeId;
            
            //if (Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_ARTIST] == null)
            //{
                int totalRecord;
                new ArtWorkBL().GetCountByItemType(itemTypeId, out totalRecord);
                //Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_ARTIST] = totalRecord;
            //}
            //model.TotalRecord = Convert.ToInt32(Session[Constants.SessionKey.SESSION_TOTAL_RECORD_PICTURES_ARTIST]);
            model.TotalRecord = totalRecord;
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = itemTypeId != null
                            ? string.Format("/art-pictures/0/{0}/{1}/{2}",
                                            itemTypeId, sort == null ? 0 : sort, Utils.UnicodeToKoDauAndGach(itemType.Name))
                            : string.Format("/0/{0}/art-pictures",
                                            sort == null ? 0 : sort);

            if (sort != null && sort == Constants.Order.ASC)
            {
                model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            }
            else
            {
                model.PageIndex = pageIndex == null ? model.TotalPage : Convert.ToInt32(pageIndex);
            }

            List<ArtWorkDetailExtend> listArtWork = new ArtWorkBL().GetByItemType(itemTypeId, model.PageIndex, pageSize, sort);
            model.ListArtWork = listArtWork;
            if (listArtWork != null && listArtWork.Count > 0 && listArtWork.Count >= model.ItemSelected)
            {
                model.ArtWorkSelected = listArtWork[Convert.ToInt32(model.ItemSelected)];
            }

            return View(model);
        }
    }
}
